﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
namespace QunYing.Common
{
    public class ImageOperatorBase : IImageOperator
    {

        public string ImageFolderPath
        {
            get;
            set;
        }

        #region IImageOperator 成员

        protected virtual string GetFolderByImageType(string imageType)
        {
            return imageType;
        }

        public virtual string[] UpLoad(string imageType, string[] imageNames, System.Drawing.Bitmap[] images)
        {
            List<string> strs = new List<string>();
            for (int i = 0; i < imageNames.Length; i++)
            {
                string path = ImageFolderPath ?? string.Empty + GetFolderByImageType(imageType) ?? string.Empty;
                path += imageNames[i] ?? string.Empty;
                try
                {
                    string dPath = Path.GetDirectoryName(path);
                    CreateDirectory(dPath);
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    images[i].Save(path);
                    images[i].Dispose();
                    strs.Add(imageNames[i]);
                }
                catch (Exception ex)
                {
                    Loger.Log.Error("上传图片时发生异常：" + ex.Message);
                }
            }
            return strs.ToArray();
        }

        protected void CreateDirectory(string dPath)
        {
            if (Directory.Exists(dPath))
            {
                return;
            }
            else
            {
                Directory.CreateDirectory(dPath);
            }
        }

        public virtual string[] Delete(string imageType, string[] imageNames)
        {
            List<string> strs = new List<string>();

            foreach (var v in imageNames)
            {
                string path = ImageFolderPath ?? string.Empty + GetFolderByImageType(imageType) ?? string.Empty;
                try
                {
                    path += v ?? string.Empty;
                    if (File.Exists(path))
                        File.Delete(path);
                    strs.Add(v);
                }
                catch (Exception ex)
                {
                    Loger.Log.Error("删除图片时发生异常：" + ex.Message);
                }
            }
            return strs.ToArray();
        }
        public virtual string[] DeleteImg(string[] imageNames)
        {
            List<string> strs = new List<string>();

            foreach (var v in imageNames)
            {
                try
                {
                    if (v.Length > 3)
                    {
                        string path =  @"D:\RuiMeiWeb\"+v;
                        if (File.Exists(path))
                            File.Delete(path);
                        strs.Add(v);
                    }
                }
                catch (Exception ex)
                {
                    Loger.Log.Error("删除图片时发生异常：" + ex.Message);
                }
            }
            return strs.ToArray();
        }

        public virtual System.Drawing.Bitmap[] GetImages(string imageType, string[] imageNames)
        {
            List<Bitmap> bitmaps = new List<Bitmap>();

            foreach (var v in imageNames)
            {
                string path = ImageFolderPath ?? string.Empty + GetFolderByImageType(imageType) ?? string.Empty;
                path += v;
                if (File.Exists(path))
                {
                    //Bitmap bm = new Bitmap(path);
                    BinaryReader binReader = new BinaryReader(File.Open(path, FileMode.Open));
                    FileInfo fileInfo = new FileInfo(path);
                    byte[] bytes = binReader.ReadBytes((int)fileInfo.Length);
                    binReader.Close();
                    Bitmap bm = new Bitmap(new MemoryStream(bytes));

                    bm.Tag = v;
                    bitmaps.Add(bm);
                }
            }
            return bitmaps.ToArray();
        }

        public virtual System.Drawing.Bitmap[] GetImgs(string[] imageNames)
        {
            List<Bitmap> bitmaps = new List<Bitmap>();

            foreach (var v in imageNames)
            {
                string path = @"D:\RuiMeiWeb\" + v;
                if (File.Exists(path))
                {
                    BinaryReader binReader = new BinaryReader(File.Open(path, FileMode.Open));
                    FileInfo fileInfo = new FileInfo(path);
                    byte[] bytes = binReader.ReadBytes((int)fileInfo.Length);
                    binReader.Close();
                    Bitmap bm = new Bitmap(new MemoryStream(bytes));
                    bm.Tag = v;
                    bitmaps.Add(bm);
                }
            }
            return bitmaps.ToArray();

            
        }

        #endregion
    }
}
